import { _decorator, Component, Label, Node } from 'cc';
const { ccclass, property } = _decorator;


@ccclass('GUIDropDownList')
export class GUIDropDownList extends Component {
    @property(Label)
    label: Label | null = null;

    @property(Node)
    optionsRoot: Node | null = null; // 假设下拉项父节点由用户预制

    private _checkFn: ((index: number, value: string) => void) | null = null;

    public init(name: string, list: string[], checkFn: (index: number, param: string) => void) {
        this._checkFn = checkFn;
        if (this.label) {
            this.label.string = name;
        }

        if (this.optionsRoot) {
            this.optionsRoot.removeAllChildren();
            list.forEach((text, index) => {
                const optionNode = new Node();
                const label = optionNode.addComponent(Label);
                label.string = text;
                optionNode.on(Node.EventType.TOUCH_END, () => {
                    this._checkFn?.(index, text);
                });
                this.optionsRoot!.addChild(optionNode);
            });
        }
    }
}